{extend name="layout/base"}

{block name="content"}

{if $username=='admin'}
<div class="layui-row">
    <button style="margin: 10px" class="layui-btn layui-btn-sm" id="addUser"><i class="layui-icon layui-icon-add-1"></i>添加</button>
    <input type="hidden" data-name="{}" id="username"/>
</div>
{else /}

{/if}


<table id="table" lay-filter="table"></table>
<script type="text/html" id="barDemo">
    {if $username == 'admin'}
        {{#  if(d.username =='admin'){ }}
        <a class="layui-btn layui-btn-sm" lay-event="editPwd"><i class="layui-icon layui-icon-edit"></i>修改密码</a>
        {{#  }else if(d.username != 'admin'){ }}
        <a class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-delete"></i>修改</a>
        <a class="layui-btn layui-btn-sm" lay-event="reset"><i class="layui-icon layui-icon-delete"></i>重置密码</a>
        <a class="layui-btn layui-btn-sm" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        <a class="layui-btn layui-btn-sm" lay-event="editPwd"><i class="layui-icon layui-icon-edit"></i>修改密码</a>
        {{#  } }}
    {else /}
        <a class="layui-btn layui-btn-sm" lay-event="editPwd"><i class="layui-icon layui-icon-edit"></i>修改密码</a>
    {/if}
</script>
<script type="text/html" id="statusTpl">
    {if $username== 'admin'}
        {{# if(d.username=='admin'){ }}
            {{# if(d.status==2){ }}
                正常
            {{# }else{ }}
                异常
             {{# } }}
        {{#} else { }}
            <input type="checkbox" name="is_lock" value="{{d.manid}}" lay-skin="switch" lay-text="正常|禁用" lay-filter="is_lock" {{ d.status == 2 ? 'checked' : '' }}>
        {{# } }}
    {else /}
        {{# if(d.status==2){ }}
        正常
        {{# }else{ }}
        异常
        {{# } }}
    {/if}
</script>

<script>
    layui.use(['jquery', 'table', 'layer', 'form'], function ($, table, layer, form) {

        $('#addUser').click(function () {
            layer.open({
                type: 2,
                content: "{:url('manager/add')}",
                area: ['600px', '400px'],
                title: '添加用户'
            });
        });

        var opt = {
                elem: "#table",
                url: "{:url('')}",
                page: true,
                cols: [[
                    {field: 'manid', title: "ID"},
                    {field: 'username', title: '用户名'},
                    //{field: 'roleName', title: '角色名称'},
                    {field: 'status', title: '状态', templet:'#statusTpl'},
                    {title: '操作', toolbar: '#barDemo',width:400}
                ]]
            },
            tabIns = table.render(opt);
        //tool()里面的参数填写table 的 elem属性
        table.on('tool(table)', function (obj) {
            var uid = obj.data.manid;
            switch (obj.event) {
                case 'editPwd':
                    layer.open({
                        type: 2,
                        title: '修改密码',
                        area: ["450px", "280px"],
                        content: "{:url('manager/editPwd')}?manid=" + uid,
                        yes:function () {
                            layer.close(opt)
                        }
                    });
                    break;
                case 'del':
                    layer.confirm('确定要删除吗', {icon: 7, title:'删除'},function (index) {
                        $.get("{:url('manager/del')}",{manid:uid});
                        layer.close(index);
                        tabIns.reload(opt)
                    });
                    break;
                case 'reset':
                    layer.confirm('重置密码为“123456”，确定要重置密码', {icon: 7, title:'重置密码'},function (index) {
                        $.get("{:url('manager/reset')}",{manid:uid});
                        layer.close(index);
                        tabIns.reload(opt)
                    });
                    break;
                case 'edit':
                    layer.open({
                        type: 2,
                        title: '修改',
                        area: ["70%", "70%"],
                        content: "{:url('edit')}?manid=" + uid,
                        yes:function () {
                            layer.close(opt)
                        }
                    });
                    break;
                default:
                    break;
            }

        })
        //状态
        form.on('switch(is_lock)', function(obj){
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var uid = this.value;
            var is_lock = obj.elem.checked === true ? 2 : -1;
            $.post('{:url("manager/editStatus")}',{'manid':uid,'status':is_lock},function (res) {
                layer.close(loading);
                if (res.code == 0) {
                    tabIns.reload(opt)
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                    return false;
                }
            })
        });
    });


</script>
{/block}